package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.JsonResult;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try{
            permissionService.reload();
        }catch (Exception e){
            return new JsonResult(false,"权限加载失败");
        }
        return new JsonResult(true,"权限加载成功");
    }
    @RequestMapping("/list")
    public String list(Model model,QueryObject qo) {
        PageInfo result = permissionService.query(qo);
        model.addAttribute("pageInfo", result);
        return "/permission/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        if (id != null) {
            model.addAttribute("dept", permissionService.get(id));
        }
        return "/permission/input";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Model model, Long id) {
        try {
            permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
        return new JsonResult(true,"删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    public String save(Permission permission) {
        if (permission.getId() != null) {
            permissionService.update(permission);
        } else {
            permissionService.save(permission);
        }
        return "redirect:/permission/list.do";
    }

}
